﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling;

namespace devtm.AutoMapper
{
    
    public static class StoreHelper
    {


        /// <summary>
        /// Load a Store for the ProjectItem
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static Store GetStore(EnvDTE.ProjectItem item)
        {

            string inputFilePath = item.FileNames[0];
            Store store = new Store(
                typeof(Microsoft.VisualStudio.Modeling.Diagrams.CoreDesignSurfaceDomainModel), 
                typeof(AutoMapperDomainModel));

            MappingModel model;

            using (Transaction transaction = store.TransactionManager.BeginTransaction("Load", true))
            {

                SerializationResult serializationResult = new SerializationResult();
                model = AutoMapperSerializationHelper.Instance.LoadModel(serializationResult, store, inputFilePath, null, null, null);
                model.FilePath = inputFilePath;
                if (serializationResult.Failed)
                    throw new SerializationException(serializationResult);

                transaction.Commit();

            }

            return store;

        }

    }


}
